package com.yami.shop.security.api.filter;

import com.yami.shop.bean.enums.SendType;
import com.yami.shop.bean.model.SubmitPerson;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.security.api.model.YamiSubmitPerson;
import com.yami.shop.security.comment.exception.YamiAuthFilterException;
import com.yami.shop.security.comment.filter.YamiAuthenticationProcessingFilter;
import com.yami.shop.service.SubmitPersonService;
import com.yami.shop.security.comment.token.AuthenticationToken;
import com.yami.shop.service.SmsLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component

public class SubmitPersonLoginAuthenticationFilter extends YamiAuthenticationProcessingFilter {

    private final SubmitPersonService submitPersonService;
    private final SmsLogService smsLogService;
    private UserDetailsService userDetailsService;

    @Autowired
    protected SubmitPersonLoginAuthenticationFilter(SubmitPersonService submitPersonService, SmsLogService smsLogService) {
        super("/submitPersonLogin");
        this.submitPersonService = submitPersonService;
        this.smsLogService = smsLogService;
    }

    @Override
    protected UserDetails getUserDetails(AuthenticationToken authentication) {
        YamiSubmitPerson yamiSubmitPerson = new YamiSubmitPerson();
        boolean validCode;
        try {
            validCode = smsLogService.checkValidCode(authentication.getPrincipal(), String.valueOf(authentication.getCredentials()), SendType.SUBMIT_PERSON_LOGIN);
        } catch (YamiShopBindException e) {
            // 验证码校验过频繁，请稍后再试
            yamiSubmitPerson.setLoginResFlag(false);
            yamiSubmitPerson.setErrorMes(I18nMessage.getMessage("yami.user.code.check.too.much"));
            return yamiSubmitPerson;
        }
        if (!validCode) {
            // 验证码有误或已过期
            yamiSubmitPerson.setLoginResFlag(false);
            yamiSubmitPerson.setErrorMes(I18nMessage.getMessage("yami.user.code.error"));
            return yamiSubmitPerson;
        }
        SubmitPerson submitPerson = submitPersonService.loadSubmitPersonByMobile(authentication.getPrincipal());
        if (Objects.isNull(submitPerson)){
            // 提报人不存在或该提报人没有可提交商品的协同采购单
            yamiSubmitPerson.setLoginResFlag(false);
            yamiSubmitPerson.setErrorMes(I18nMessage.getMessage("yami.submit.person.account.no.exist"));
            return yamiSubmitPerson;
        }
        yamiSubmitPerson.setLoginResFlag(true);
        yamiSubmitPerson.setSubmitPersonId(submitPerson.getId());
        yamiSubmitPerson.setMobile(submitPerson.getMobile());
        yamiSubmitPerson.setStatus(submitPerson.getStatus());
        return yamiSubmitPerson;
    }


}
